﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceCollectionDomain.Entity;

namespace ResourceCollectionDao
{
   public class ToDoDao
    {
       /// <summary>
       /// 将TODOURLs保存到数据库
       /// </summary>
        /// <param name="urls"></param>
       /// <param name="type"></param>
       public static void AddUrls(List<string> urls ,string type)
       {
           using (var db = new DBDataContext())
           {
               List<ToDo> list=new List<ToDo>();
               foreach (string url in urls)
               {
                   ToDo todo = new ToDo();
                   todo.CreateOn = DateTime.Now;
                   todo.PageURL = url;
                   todo.Type = type;
                   list.Add(todo);
               }
               db.ToDo.InsertAllOnSubmit(list);
               db.SubmitChanges();
           }
       }

       public static void DeleteAll()
       {
           using (var db = new DBDataContext())
           {
               db.ExecuteCommand("delete from ToDo");
           }
       }
       /// <summary>
       /// 获取某种类型URLs
       /// </summary>
       /// <param name="type"></param>
       /// <returns></returns>
       public static string[] GetAll(string type=null)
       {
           using (var db = new DBDataContext())
           {
               if (!string.IsNullOrWhiteSpace(type))
                   return db.ToDo.Where(x => x.Type == type).Select(x => x.PageURL).ToArray();
               return db.ToDo.Select(x => x.PageURL).ToArray();
           }
       } 
    }
}
